// Original Author: Marc Belmont <code at marcbelmont.com>
// Contributors:

///////////////
// CONSTANTS //
///////////////

const CALCULATOR_PREFERENCES = "preferences";
const PREF_NAME_ANGLE = "calc_angle";
const PREF_NAME_CALC_PAD = "calc_pad";
const PREF_NAME_CALC_OPEN = "calc_open";
const PREF_NAME_FORMAT = "calc_format";
const PREF_NAME_READABILITY = "calc_readability";
const PREF_UPDATED = "calc_updated";

const DEGREE = 0;
const RADIAN = 1;
const GRAD = 2;

const PAD_YES = 0;
const PAD_NO = 1;

const FORMAT_EN = 0;
const FORMAT_FR = 1;

///////////
// Class //
///////////

var preferences = null;

function loadUI() {
  preferences = new Preferences();
  preferences.init();
  preferences.loadUI();
}

function Preferences() {
  return this;
}

Preferences.prototype = {
  mBranch: null,

  init : function() {
    var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    this.mBranch = prefService.getBranch("calculator.");
    this.loadAll();
  },

  ////////////////////////////////
  // Setters and getters (core) //
  ////////////////////////////////

  getPreference : function(aPref, aType, aDefault) {
    try {
      var val = eval("this.mBranch.get" + aType + "Pref('" + aPref + "')");
      return val;
    } catch(e) {
      this.setPreference(aPref, aType, aDefault);
      return aDefault;
    }
    return null;
  },

  setPreference : function(aPref, aType, aValue) {
    try {
      eval("this.mBranch.set" + aType + "Pref('" + aPref + "'," + aValue + ")");
      return true;
    } catch(e) {
      alert(e);
    }
    return false;
  },

  //////////
  // Misc //
  //////////

  accept : function() {
    this.setPreference(PREF_NAME_ANGLE, "Int", document.getElementById("calculator-group-units").selectedIndex);
    this.setPreference(PREF_NAME_CALC_PAD, "Int", document.getElementById("calculator-showpad").selectedIndex);
    this.setPreference(PREF_NAME_CALC_OPEN, "Int", document.getElementById("calculator-launch").selectedIndex);
    this.setPreference(PREF_NAME_FORMAT, "Int", document.getElementById("calculator-format").selectedIndex);
    this.setPreference(PREF_NAME_READABILITY, "Int", document.getElementById("readability").selectedIndex);
  },

  loadAll : function() {
    this.getPreference(PREF_NAME_ANGLE, "Int", RADIAN);
    this.getPreference(PREF_NAME_CALC_PAD, "Int", PAD_YES);
    this.getPreference(PREF_NAME_CALC_OPEN, "Int", 0);
    this.getPreference(PREF_NAME_FORMAT, "Int", FORMAT_EN);
    this.getPreference(PREF_NAME_READABILITY, "Int", 1);
    return true;
  },

  ////////
  // UI //
  ////////

  loadUI : function() {
    document.getElementById("calculator-group-units").selectedIndex = this.getPreference(PREF_NAME_ANGLE, "Int");
    document.getElementById("calculator-showpad").selectedIndex = this.getPreference(PREF_NAME_CALC_PAD, "Int");
    document.getElementById("calculator-launch").selectedIndex = this.getPreference(PREF_NAME_CALC_OPEN, "Int");
    document.getElementById("calculator-format").selectedIndex = this.getPreference(PREF_NAME_FORMAT, "Int");
    document.getElementById("readability").selectedIndex = this.getPreference(PREF_NAME_READABILITY, "Int");
  }
}
